import 'package:number_spark_flutter/config/ns_base_controller.dart';
import 'package:number_spark_flutter/model/ns_coin_goods_model.dart';
import 'package:number_spark_flutter/network/ns_api.dart';
import 'package:number_spark_flutter/pages/ns_mine/ns_mine_logic.dart';
import 'package:number_spark_flutter/utils/ns_inapp_purchase_util.dart';
import 'package:number_spark_flutter/utils/ns_log_report_utils.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter_inapp_purchase/flutter_inapp_purchase.dart';
import 'package:get/get.dart';

class NSCoinShopLogic extends NSBaseController {
  List<NSCoinGoodsModel> goodsList = [];

  @override
  void onInit() {
    super.onInit();
    onGetCoinShopList();
    NSLogReportUtils.stat(page: NSLogPages.nsPurchase);
  }

  onGetCoinShopList() async {
    Map<String, dynamic> params = {'isIncludeSubscription': false, 'payChannel': 'IAP'};
    NSResponseData responseData = await NSApi.coinShopList.post(params);
    if (responseData.isSuccess()) {
      print('responseData.data = ${responseData.data}');
      List list = responseData.data;
      goodsList = list.map((e) => NSCoinGoodsModel.fromJson(e)).toList();
      update();
    }
  }

  doClickItem(NSCoinGoodsModel model) async {
    BotToast.showLoading();
    Map<String, dynamic> params = {
      'entry': '',
      'source': '',
      'goodsCode': model.code,
      'payChannel': 'IAP',
    };
    NSResponseData responseData = await NSApi.createOrder.post(params);
    if (responseData.isSuccess()) {
      NSLogReportUtils.stat(page: NSLogPages.nsCreateorder);
      print('responseData.data = ${responseData.data}');
      NSInAppPurchaseModel purchaseModel = NSInAppPurchaseModel(
        productId: model.code,
        orderNo: responseData.data['orderNo'],
        tradeNo: responseData.data['tradeNo'],
        successBlock: () {
          NSMineLogic logic = Get.find();
          logic.onGetUserInfo();
        },
      );
      NSInAppPurchaseUtil().getProduct(purchaseModel);
      // NSInAppPurchaseUtil.checkOrderForService(null);
    } else {
      BotToast.closeAllLoading();
    }
  }
}
